from datetime import datetime

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone

from martor.models import MartorField

# from core.models import CustomUser
from basic_info.models import Station, Personnel


class LabLog(models.Model):
    '''
    机房日志信息
    '''
    weather_type = (
        ('sunny', '晴'),
        ('cloudy', '阴'),
        ('rain', '雨'),
    )

    time_slot_choice = (
        (1, '上午'),
        (2, '下午'),
    )
    status_choice = (
        (1, '正常'),
        (2, '部分网络设备故障'),
        (3, '网络瘫痪'),
        (4, '其它'),
    )

    lab_template = models.FloatField(default=0.0, verbose_name='机房温度(°)')
    lab_humidity = models.FloatField(default=0.0, verbose_name='机房湿度(°)')

    hc_room_template = models.FloatField(default=0.0, verbose_name='人机交互室温度(°)')
    hc_room_humidity = models.FloatField(default=0.0, verbose_name='人机交互室湿度(°)')

    network_delay = models.IntegerField(default=15, verbose_name='网络延迟(ms)')
    packet_lose_rate = models.FloatField(default=0.0, verbose_name='信息节点掉包率(%)')

    time_slot = models.IntegerField(choices=time_slot_choice, verbose_name='记录时段')

    weather = models.CharField(max_length=6, choices=weather_type, default='sunny', verbose_name='天气情况')

    log_date = models.DateField(verbose_name='日志日期', default=timezone.now)

    log_time = models.TimeField(default=datetime.now(), verbose_name='记录时间点')

    description = MartorField(blank=True, verbose_name='描述')

    created_at = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
    last_updated = models.DateTimeField(auto_now=True, verbose_name='最后更新时间')

    created_by = models.ForeignKey(User, null=True, on_delete=models.CASCADE, verbose_name='发布人',
                                   related_name='lablogs')
    updated_by = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, verbose_name='更新人',
                                   related_name='+')

    class Meta:
        verbose_name = '机房值班日志'
        verbose_name_plural = '机房值班日志'
        unique_together = ("log_date", "time_slot")


class StationDataLog(models.Model):
    '''
    小孔径台网五个子台数据连续率
    '''
    station = models.ForeignKey(Station, on_delete=models.CASCADE, verbose_name='所属台站')
    online_data = models.FloatField(default=1.0, verbose_name='台站数据连续率(网络)')
    offline_data = models.FloatField(default=1.0, verbose_name='台站数据连续率(离线)')
    description = MartorField()

    log_date = models.DateField(verbose_name='日志日期')

    created_at = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
    last_updated = models.DateTimeField(auto_now=True, verbose_name='最后更新时间')

    created_by = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='值班人', related_name='datalogs')
    updated_by = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, verbose_name='更新者',
                                   related_name='+')

    class Meta:
        verbose_name = '台站维护日志'
        verbose_name_plural = '台站维护日志'


class StationCalibration(models.Model):
    '''
    小孔径台网五个子台标定设置情况
    '''
    station = models.ForeignKey(Station, on_delete=models.CASCADE, verbose_name='所属台站')
    trigger_time = models.DateTimeField(verbose_name='标定触发时间')


class StationCalibrationLog(models.Model):
    '''
    小孔径台网五个子台标定日志
    '''
    station = models.ForeignKey(Station, on_delete=models.CASCADE, verbose_name='所属台站')
    status = models.BooleanField(default=False, verbose_name='标定状态')

    is_cut = models.BooleanField(default=False, verbose_name='是否截取')

    is_manual_trigger = models.BooleanField(default=False, verbose_name='是否手动触发')
    manual_trigger_time = models.DateTimeField(verbose_name='标定手动触发时间')

    created_at = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
    last_updated = models.DateTimeField(auto_now=True, verbose_name='最后更新时间')

    created_by = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='值班人', related_name='calibrationlogs')
    updated_by = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, verbose_name='更新者',
                                   related_name='+')

    description = MartorField()


class StationMaintenanceLog(models.Model):
    '''
    小孔径台网五个子台现场维护日志
    '''
    station = models.ForeignKey(Station, on_delete=models.CASCADE, verbose_name='所属台站', related_name='maintenancelogs')
    maintenance_date = models.DateField(verbose_name='维护日期')
    maintainer = models.ManyToManyField(Personnel, verbose_name='维护人', related_name='maintenances')

    created_at = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
    last_updated = models.DateTimeField(auto_now=True, verbose_name='最后更新时间')

    created_by = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='发布人', related_name='maintenancelogs')
    updated_by = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, verbose_name='更新者',
                                   related_name='opmaintenancelogs')
    description = MartorField(verbose_name='描述')

    class Meta:
        verbose_name = '台站维护日志'
        verbose_name_plural = '台站维护日志'

    def __str__(self):
        return "{}({})维护记录".format(self.station, self.maintenance_date)


class ShiftSchedule(models.Model):
    duty_date = models.DateField(unique=True, verbose_name='值班日期')
    arrange_duty_person = models.ForeignKey(Personnel, null=True, on_delete=models.SET_NULL, verbose_name='安排值班人',
                                            related_name='+')
    actual_duty_person = models.ForeignKey(Personnel, null=True, on_delete=models.SET_NULL, verbose_name='真实值班人',
                                           related_name='schedules')
